package com.zyc.util;

import java.util.Properties;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConfigPropertiesUtil {
	private static final Logger logger = LoggerFactory.getLogger(ConfigPropertiesUtil.class);
	private static Properties properties;
	static {
		loadProperties();
	}
	synchronized private static void loadProperties() {
		InputStream in = null;
		String propertiesFilePath = "config.properties";
		logger.info("加载properties文件");
		//Resource resource = new ClassPathResource(propertiesFilePath);
		try {
			 in = ConfigPropertiesUtil.class.getClassLoader().getResourceAsStream(propertiesFilePath);
			 properties = new Properties();
			 properties.load(in);
		} catch (FileNotFoundException e) {
			logger.warn(propertiesFilePath + "未找到");
			e.printStackTrace();
		}catch (IOException e) {
			logger.warn(e.getMessage());
			e.printStackTrace();
		}finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception e2) {
				logger.error("jdbc.properties文件流关闭出现异常");
			}
		}
		logger.info("加载properties文件:" + propertiesFilePath);
	}
	public static String getProperties(String key) {
		if(null == properties) {
			loadProperties();
		}
		return properties.getProperty(key);
	}
	/**
	 * value is not null  and the value equals properties.get(key).
	 * @param key
	 * @param value
	 * @return
	 */
	public static boolean getPropertiesEquals(String key,String value) {
		if(null == properties) {
			loadProperties();
		}
		if(null != value && value.equals(properties.get(key))) {
			return true;
		}else {
			return false;
		}
	}
}
